package com.lineage.server.command.executor;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_Lawful;
import com.lineage.server.serverpackets.S_OwnCharStatus;
import com.lineage.server.serverpackets.S_SPMR;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Status implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1Status.class);
  
  private static final int _max_int = 20000;
  
  public static L1CommandExecutor getInstance() {
    return new L1Status();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      if (pc == null)
        _log.warn("系统命令执行: " + cmdName + " " + arg + " 重置指定人物属性。"); 
      StringTokenizer st = new StringTokenizer(arg);
      String char_name = st.nextToken();
      String param = st.nextToken();
      int value = Integer.parseInt(st.nextToken());
      String e1 = "指令异常: 指定人物不在线上，这个命令必须输入正确人物名称才能执行。";
      L1PcInstance target = null;
      if (char_name.equalsIgnoreCase("me")) {
        if (pc == null) {
          _log.error("指令异常: 指定人物不在线上，这个命令必须输入正确人物名称才能执行。");
          return;
        } 
        target = pc;
      } else {
        target = World.get().getPlayer(char_name);
      } 
      if (target == null) {
        if (pc == null) {
          _log.error("指令异常: 指定人物不在线上，这个命令必须输入正确人物名称才能执行。");
          return;
        } 
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(73, char_name));
        return;
      } 
      if (param.equalsIgnoreCase("AC")) {
        target.addAc((byte)(value - target.getAc()));
      } else if (param.equalsIgnoreCase("MR")) {
        target.addMr((short)(value - target.getMr()));
        target.sendPackets((ServerBasePacket)new S_SPMR(target));
      } else if (param.equalsIgnoreCase("HIT")) {
        target.addHitup((short)(value - target.getHitup()));
      } else if (param.equalsIgnoreCase("DMG")) {
        target.addDmgup((short)(value - target.getDmgup()));
      } else if (param.equalsIgnoreCase("SP")) {
        target.addSp((short)(value - target.getSp()));
        target.sendPackets((ServerBasePacket)new S_SPMR(target));
      } else {
        if (param.equalsIgnoreCase("HP")) {
          if (value > 20000)
            value = 20000; 
          int maxHP = value - target.getBaseMaxHp();
          if (target.getBaseMaxHp() + maxHP > 20000)
            maxHP = 20000 - target.getBaseMaxHp(); 
          target.addBaseMaxHp((short)maxHP);
          target.setCurrentHpDirect(target.getMaxHp());
        } else if (param.equalsIgnoreCase("MP")) {
          if (value > 20000)
            value = 20000; 
          int maxMP = value - target.getBaseMaxMp();
          if (target.getBaseMaxMp() + maxMP > 20000)
            maxMP = 20000 - target.getBaseMaxMp(); 
          target.addBaseMaxMp((short)maxMP);
          target.setCurrentMpDirect(target.getMaxMp());
        } else if (param.equalsIgnoreCase("LAWFUL") || param.equalsIgnoreCase("L")) {
          target.setLawful(value);
          target.sendPacketsAll((ServerBasePacket)new S_Lawful(target));
        } else if (param.equalsIgnoreCase("KARMA") || param.equalsIgnoreCase("K")) {
          target.setKarma(value);
        } else if (param.equalsIgnoreCase("GM")) {
          if (value > 200)
            value = 200; 
          target.setAccessLevel((short)value);
          target.sendPackets((ServerBasePacket)new S_SystemMessage("取得GM权限"));
        } else if (param.equalsIgnoreCase("STR")) {
          target.addBaseStr((byte)(value - target.getBaseStr()));
        } else if (param.equalsIgnoreCase("CON")) {
          target.addBaseCon((byte)(value - target.getBaseCon()));
        } else if (param.equalsIgnoreCase("DEX")) {
          target.addBaseDex((byte)(value - target.getBaseDex()));
        } else if (param.equalsIgnoreCase("INT")) {
          target.addBaseInt((byte)(value - target.getBaseInt()));
          target.sendPackets((ServerBasePacket)new S_SPMR(target));
        } else if (param.equalsIgnoreCase("WIS")) {
          target.addBaseWis((byte)(value - target.getBaseWis()));
        } else if (param.equalsIgnoreCase("CHA")) {
          target.addBaseCha((byte)(value - target.getBaseCha()));
        } else {
          String e2 = "指令异常: 指令 " + param + " 不明确";
          if (pc == null) {
            _log.error(e2);
            return;
          } 
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(e2));
          return;
        } 
        target.save();
      } 
      target.sendPackets((ServerBasePacket)new S_OwnCharStatus(target));
      String ok = String.valueOf(String.valueOf(target.getName())) + "的" + param + "属性" + value + "变更完成";
      if (pc == null) {
        _log.info(ok);
        return;
      } 
      pc.sendPackets((ServerBasePacket)new S_SystemMessage(ok));
    } catch (Exception e3) {
      if (pc == null) {
        _log.error("错误的命令格式: " + getClass().getSimpleName());
      } else {
        _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
      } 
    } 
  }
}
